package com.mz.ozy.mvp.presenter;

import android.content.Context;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.DynamicListBean;
import com.mz.ozy.bean.HomePageBean;
import com.mz.ozy.mvp.model.ExpertModel;
import com.mz.ozy.mvp.model.FindDynamicsModel;
import com.mz.ozy.mvp.model.FollowModel;
import com.mz.ozy.mvp.view.ExpertDetailContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import okhttp3.ResponseBody;

/**
 * Created by administrator on 2018/12/3.
 */
public class ExpertDetailPresenter extends BasePresenterImpl<ExpertDetailContract.View> implements ExpertDetailContract.Presenter {

    private ExpertModel mExpertModel;
    private FindDynamicsModel mFindDynamicsModel;
    private FollowModel mFollowModel;

    public ExpertDetailPresenter() {
        mExpertModel = new ExpertModel();
        mFollowModel = new FollowModel();
        mFindDynamicsModel = new FindDynamicsModel();
    }

    @Override
    public void queryExpertDetail(Context context, String uid, int pageNo, int dynamic) {
        mExpertModel.queryExpertDetail(context, uid, pageNo, dynamic)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<DynamicListBean>(mView) {

                    @Override
                    public void onSuccesss(DynamicListBean jsonObject) {
                        mView.getExpertDetail(jsonObject);
                    }
                });
    }

    @Override
    public void queryHomePage(Context context, String uid, int pageNo, int dynamic) {
        mExpertModel.queryHomePage(context, uid, pageNo, dynamic)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<HomePageBean>(mView) {

                    @Override
                    public void onSuccesss(HomePageBean jsonObject) {
                        mView.getHomePage(jsonObject);
                    }
                });
    }

    @Override
    public void followSportsMan(Context context, int oid, int sType) {
        mFollowModel.followTheObject(context, oid, sType)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.followSportsSuccess(jsonObject);
                    }
                });
    }

    @Override
    public void deFriend(Context context, String did) {
        mExpertModel.deFriend(context, did)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.deFriend(jsonObject);
                    }
                });
    }

    @Override
    public void followDynamics(Context context, int id, int type) {
        mFollowModel.followTheObject(context, id, type).
                compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.followDynamicsSuccess(jsonObject);
                    }
                });
    }

    public void deleteDynamics(Context context, int id) {
        mFindDynamicsModel.deleteDynamics(context, id).
                compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.deleteDynamicsSuccess(jsonObject);
                    }
                });
    }


    @Override
    public void commentDynamics(Context context, int type, String cid, String pid, String uuid, String content) {
        mFindDynamicsModel.commentDynamics(context, type, cid, pid, uuid, content)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject result) {
                        mView.commentDynamicsSuccess(result);
                    }
                });
    }
}
